package cn.edu.ujn.lab3.service.impl;

import cn.edu.ujn.lab3.dao.Products;
import cn.edu.ujn.lab3.dao.ProductsMapper;
import cn.edu.ujn.lab3.service.ProductsService;
import cn.edu.ujn.lab3.util.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("productsService")
@Transactional
public class ProductsServiceImpl implements ProductsService {

    @Autowired
    private ProductsMapper productsDao;

    //查询产品列表
    @Override
    public Page<Products> findProductsList(Integer page, Integer rows, String prodName, String prodPrice, String prodCategories, String prodState, String prodRemark) {
        Products products = new Products();
        if (StringUtils.isNotBlank(prodName)) {
            products.setProdName(prodName);
        }
        if (StringUtils.isNotBlank(prodPrice)) {
            products.setProdPrice(prodPrice);
        }
        if (StringUtils.isNotBlank(prodCategories)) {
            products.setProdCategories(prodCategories);
        }
        if (StringUtils.isNotBlank(prodState)) {
            products.setProdState(prodState);
        }
        if (StringUtils.isNotBlank(prodRemark)) {
            products.setProdRemark(prodRemark);
        }

        products.setStart((page - 1) * rows);
        products.setRows(rows);

        List<Products> productsList = productsDao.selectProductList(products);
        Integer count = productsDao.selectProductsListCount(products);
        Page<Products> result = new Page<Products>();
        result.setPage(page);
        result.setRows(productsList);
        result.setSize(rows);
        result.setTotal(count);
        return result;
    }
    //产品创建
    @Override
    public int createProducts(Products products) {
        return productsDao.insertSelective(products);
    }
    //通过id获取产品
	@Override
	public Products getProductsById(Integer id) {
		return productsDao.selectByPrimaryKey(id);
	}
	//更新产品
	@Override
	public int updateProducts(Products products) {
		return productsDao.updateByPrimaryKeySelective(products);
	}
	//删除产品
	@Override
	public int deleteProducts(Integer id) {
		return productsDao.deleteByPrimaryKey(id);
	}
	//根据产品名字查询产品
	@Override
	public Products getProductsByName(String prodName) {
		// TODO Auto-generated method stub
		return productsDao.selectByName(prodName);
	}

}